package com.hugo.maomi_first.zhuanti.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hugo.maomi_first.R;
import com.hugo.maomi_first.base.BaseMvpFragment;
import com.hugo.maomi_first.base.adapter.BaseRcQuickAdapter;
import com.hugo.maomi_first.bean.ZhuanTiBean;
import com.hugo.maomi_first.bean.ZhuanTiDetailBean;
import com.hugo.maomi_first.bean.ZhuanTiHeaderBean;
import com.hugo.maomi_first.zhuanti.adapter.ArticleAdapter;
import com.hugo.maomi_first.zhuanti.adapter.RecyclerAdapter;
import com.hugo.maomi_first.zhuanti.presenter.ZhuanTiPresenterImpl;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


public class ZhuantiFragment extends BaseMvpFragment<IZhuantiView, ZhuanTiPresenterImpl>
        implements IZhuantiView, XRecyclerView.LoadingListener {

    private RecyclerAdapter adapter;

    private List<ZhuanTiBean.DataBean.ItemsBean> listdetail = new ArrayList<>();

    private List<ZhuanTiHeaderBean.DataBean.ItemsBean> listheader = new ArrayList<>();


    private int pager=1;

    HeadViewHolder hvh;

    private ArticleAdapter ariticleAdapter;


    @Bind(R.id.xrecyclerView_zhuanti_main)
    XRecyclerView xrecyclerViewZhuantiMain;

    public static ZhuantiFragment newInstance() {

        Bundle args = new Bundle();

        ZhuantiFragment fragment = new ZhuantiFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_zhuanti;
    }

    @Override
    public void initView() {
        //头部View的辅助类
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        xrecyclerViewZhuantiMain.setLayoutManager(layoutManager);

        //设置刷新的样式
        xrecyclerViewZhuantiMain.setRefreshProgressStyle(ProgressStyle.SquareSpin);

        //设置自动刷新
        xrecyclerViewZhuantiMain.setLoadingListener(this);

        //设置适配器
        ariticleAdapter=new ArticleAdapter(context, R.layout
                .item_xrecyclerview_zhuanti);
        xrecyclerViewZhuantiMain.setAdapter(ariticleAdapter);

        //加载Header视图的图片
        presenter.getZhuanTiHeaderData();

        hvh=new HeadViewHolder();

        //添加头部View
        xrecyclerViewZhuantiMain.addHeaderView(hvh.headView);
        /*首次刷新*/
        xrecyclerViewZhuantiMain.setRefreshing(true);

        //跳转到详情页面
        toDetail();
    }

    private void toDetail() {
        ariticleAdapter.setOnItemClickListener(new BaseRcQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent();
                intent.putExtra("sub",ariticleAdapter.getItem(position).getId());
                intent.setClass(getActivity(),ZhuanTiDetailActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    /**
     * 头视图配置适配器
     * @param list
     */
    @Override
    public void onLoadHeader(List<ZhuanTiHeaderBean.DataBean.ItemsBean> list) {

        adapter=new RecyclerAdapter(getActivity(),list);
        hvh.recyclerViewHeader.setAdapter(adapter);

    }

    @Override
    public void onLoadDetail(List<ZhuanTiBean.DataBean.ItemsBean> list) {
        if (pager==1){
            ariticleAdapter.replaceAll(list);
            //加载完成后刷新取消
            xrecyclerViewZhuantiMain.refreshComplete();
        }else {
            ariticleAdapter.addAll(list);
            xrecyclerViewZhuantiMain.loadMoreComplete();
        }
        listdetail.addAll(list);



    }

    @Override
    public void onLoadZhuanTiDetail(ZhuanTiDetailBean.DataBean bean) {

    }

    @Override
    public void onFaile() {

    }

    @Override
    protected ZhuanTiPresenterImpl initPresenter() {
        return new ZhuanTiPresenterImpl();
    }

    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {
        pager=1;
        presenter.getZhuanTiData(pager);
    }

    /**
     * 上拉加载
     */
    @Override
    public void onLoadMore() {
        pager++;
        presenter.getZhuanTiData(pager);


    }


    class HeadViewHolder {
        public View headView;
        @Bind(R.id.recyclerView_header)
        RecyclerView recyclerViewHeader;

        public HeadViewHolder() {
            this.headView = inflater.inflate(R.layout.recyclerview_zhuanti,
                    xrecyclerViewZhuantiMain, false);
            ButterKnife.bind(this, headView);

            recyclerViewHeader.setHasFixedSize(true);
            GridLayoutManager manger=new GridLayoutManager(context,4);
            recyclerViewHeader.setLayoutManager(manger);



        }
    }
}
